package org.springblade.task.job.report.sales;

import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springblade.common.utils.CombineMapUtil;
import org.springblade.common.utils.DateUtils;
import org.springblade.modules.salesAnalysis.entity.DailyOrderAmountRangeReport;
import org.springblade.modules.salesAnalysis.service.IDailyOrderAmountRangeReportService;
import org.springblade.task.base.param.BaseOrderBookParam;
import org.springblade.task.base.service.IBaseAudioChapterOrderService;
import org.springblade.task.base.service.IBaseEbookOrderService;
import org.springblade.task.base.service.IBaseNetChapterOrderService;
import org.springblade.task.job.base.BaseTaskInfoJob;
import org.springblade.task.quartz.entity.QuartzTask;
import org.springblade.task.quartz.service.IQuartzTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Title: 订单金额分布
 * @version V1.0
 */
@Component
public class DailyOrderAmountRangeReportJob {

	private static Logger logger = LoggerFactory.getLogger(BaseTaskInfoJob.class);

	@Autowired
	private IQuartzTaskService quartzTaskService;

	@Autowired
	private IBaseEbookOrderService BaseEbookOrderService;
	@Autowired
	private IBaseAudioChapterOrderService BaseAudioChapterOrderService;
	@Autowired
	private IBaseNetChapterOrderService BaseNetChapterOrderService;
	@Autowired
	private IDailyOrderAmountRangeReportService dailyOrderAmountRangeReportService;

	/**
	 * 1、简单任务示例（Bean模式）
	 */
	@XxlJob("dailyOrderAmountRangeReportJob")
	public ReturnT<String> dailyOrderAmountRangeReportJob(String param) throws Exception {
		System.out.println(DateUtils.currentDateTime() + " -- XXL-JOB, Hello World.");
		System.err.println("执行静态定时任务日期: " + DailyOrderAmountRangeReportJob.class.getSimpleName());
		QuartzTask quartzTask = quartzTaskService.queryByTaskName(DailyOrderAmountRangeReportJob.class.getSimpleName(), -7);
		// 今日
		String todayDate = DateUtils.crutDate();
		String taskDate = quartzTask.getTaskDate();
		while (taskDate.compareTo(todayDate) < 0) {
			// 当时间大于5点，跳出执行
//			if (DateUtils.getHour24() > 5) {
//				break;
//			}
			// 再加一个凌晨1点判断
			currMethod(taskDate);
			// 增加1天
			taskDate = DateUtils.dateAddDay(taskDate, 1);
		}
		quartzTask.setTaskDate(taskDate);
		quartzTaskService.saveQuartzTask(quartzTask);
		return ReturnT.SUCCESS;
	}

	/**
	 *
	 * @param taskDate
	 */
	public void currMethod(String taskDate) {

		BaseOrderBookParam param = new BaseOrderBookParam();
		param.setStartDate("2010-08-25 00:00:00");//DateUtils.getDateStartTime(taskDate));
		param.setEndDate(DateUtils.getDayEndTime(taskDate));
		Map<String, BigDecimal> orderBookMap =  BaseEbookOrderService.queryOrderAmountRange(param);
		Map<String, BigDecimal> orderAudioBookMap = BaseAudioChapterOrderService.queryOrderAmountRange(param);
		Map<String, BigDecimal> orderOnlineBookMap = BaseNetChapterOrderService.queryOrderAmountRange(param);
		Map<String, BigDecimal> result1 = CombineMapUtil.addTo(orderBookMap, orderAudioBookMap);
		Map<String, BigDecimal> result2 = CombineMapUtil.addTo(result1, orderOnlineBookMap);

		String orderAmountRange_5 = "5元";
		String orderAmountRange_5_10 = "5~10元";
		String orderAmountRange_10_15 = "10~15元";
		String orderAmountRange_15 = "15+元";
		String order_num = "order_num_";
		String order_amount = "order_amount_";
		DailyOrderAmountRangeReport report_5 = new DailyOrderAmountRangeReport();
		report_5.setDate(taskDate);
		report_5.setOrderAmountRange(orderAmountRange_5);
		BigDecimal o = result2.get(order_num + "5");
		report_5.setOrderNum(result2.get(order_num + "5") == null ? 0 : result2.get(order_num + "5").intValue());
		report_5.setOrderAmount(result2.get(order_amount + "5") == null ? 0 : result2.get(order_amount + "5").intValue());

		DailyOrderAmountRangeReport report_5_10 = new DailyOrderAmountRangeReport();
		report_5_10.setDate(taskDate);
		report_5_10.setOrderAmountRange(orderAmountRange_5_10);
		report_5_10.setOrderNum(result2.get(order_num + "5_10") == null ?  0 : result2.get(order_num + "5_10").intValue());
		report_5_10.setOrderAmount(result2.get(order_amount + "5_10") == null ? 0 : result2.get(order_amount + "5_10").intValue());

		DailyOrderAmountRangeReport report_10_15 = new DailyOrderAmountRangeReport();
		report_10_15.setDate(taskDate);
		report_10_15.setOrderAmountRange(orderAmountRange_10_15);
		report_10_15.setOrderNum(result2.get(order_num + "10_15") == null ? 0 : result2.get(order_num + "10_15").intValue());
		report_10_15.setOrderAmount(result2.get(order_amount + "10_15") == null ? 0 : result2.get(order_amount + "10_15").intValue());

		DailyOrderAmountRangeReport report_15 = new DailyOrderAmountRangeReport();
		report_15.setDate(taskDate);
		report_15.setOrderAmountRange(orderAmountRange_15);
		report_15.setOrderNum(result2.get(order_num + "15") == null ? 0 : result2.get(order_num + "15").intValue());
		report_15.setOrderAmount(result2.get(order_amount + "15") == null ? 0 : result2.get(order_amount + "15").intValue());

		List<DailyOrderAmountRangeReport> reportList = new ArrayList<>();
		reportList.add(report_5);
		reportList.add(report_5_10);
		reportList.add(report_10_15);
		reportList.add(report_15);

		dailyOrderAmountRangeReportService.saveBatchTaskReport(reportList);
	}


	public void init() {
		logger.info("init");
	}

	public void destroy() {
		logger.info("destory");
	}

}
